using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using EntLibContrib.Validation.Configuration;

namespace EntLibContrib.Validation
{

    [ConfigurationElementType(typeof(ExternallyConfiguredObjectCollectionValidatorData))]
    public class ExternallyConfiguredObjectCollectionValidator : ExternallyConfiguredObjectValidator
    {
        public ExternallyConfiguredObjectCollectionValidator(string ruleSet, string configurationFilePath)
            : base(ruleSet, configurationFilePath)
        {
        }

        protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate != null)
            {
                IEnumerable enumerable = objectToValidate as IEnumerable;
                if (enumerable != null)
                {
                    foreach (object element in enumerable)
                    {
                        base.DoValidate(element, element, null, validationResults);
                    }
                }
                else
                {
                    this.LogValidationResult(validationResults, Resources.ValidationErrorIncompatibleType, currentTarget, key);
                }
            }
        }

    }
}
